package app.agent.presentation

import im.mange.jetboot._
import im.mange.jetboot.bootstrap3.Bootstrap
import jetboot._
import Html._
import Bootstrap._
import Css._
import im.mange.shoreditch.hipster.Check
import scala.Some
import app.server.services.ServiceFactory.systemClock
import im.mange.shoreditch.engine.{Clock, DateFormatForHumans}

//TODO: need a live or static mode option
case class CheckRenderer(c: Check, reasons: List[String])(implicit clock: Clock) extends Renderable {
  def render = Composite(
    div(
//      span(span().classes(classMeUp).styles(marginRight("5px")),
      span(
        Composite(span(c.id.toString).classes("badge").styles(fontSize(xSmall)), R(" " + c.pure + " ")), span(c.replacedParams.mkString(" ") + " ").styles(fontSize(smaller), color("darkgray"))
      ).classes(pullLeft),
      SystemRenderer(c.in)
    ).styles(clear(both)),
    div(
      span(StatusRenderer(status, c.script.isCurrentlyRunningStep(c)), R(when + message)).classes(pullLeft).styles(fontSize(xSmall), marginTop("3px"))
    ).styles(clear(both))
  ).render

  private def when = if (c.script.hasStarted && (c.script.isCurrentlyRunningStep(c) || c.isCompleted)) DateFormatForHumans.formattedNow(clock) + " " else ""

  private def message = {
    val m = if (reasons.isEmpty && c.script.isCurrentlyRunningStep(c)) "Running..."
    else reasons.mkString(",")
    if (!m.isEmpty) "- " + m else m
  }

  private def status =
    if (!reasons.isEmpty) Some(false)
    else if (c.isCompleted) Some(true)
    else None
}
